package com.ustadzmenjawab.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;

import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import com.reveregroup.gwt.facebook4gwt.ShareButton;
import com.ustadzmenjawab.client.SearchResultPage.SearchValueChangeHandler;
import com.ustadzmenjawab.client.images.WebImages;
import com.ustadzmenjawab.shared.SearchResult;

public class MobileSearchPage extends SearchResultPage {
	
	
	public MobileSearchPage(String keyword, String[] sources, SearchResult[] msg) {
	
		scrWidthResolution = getScreenWidth();
		scrHeightResolution = getScreenHeight();
		basePanel= new DockPanel();
		searchButton = new Button("Cari");
		searchBox = new TextBox();
		centralPanel = new VerticalPanel() ;
		
		searchBox.setWidth(((scrWidthResolution * 4 )/5) +"px");
		searchButton.setPixelSize(80, 25);
		topPanel = new VerticalPanel();
		Panel titleHorzPanel = new HorizontalPanel();
		HorizontalPanel buttonHorzPanel = new HorizontalPanel();
		titleHorzPanel.add(new HTML("<font color=\"#7fbce8\">Ustadz Menjawab</font>"));
		//fbShare = new ShareButton("http://www.ustadzmenjawab.com","Search engine tanya-jawab syariah islam");
		topPanel.add(titleHorzPanel);
		topPanel.add(searchBox);
		buttonHorzPanel.setWidth("100%");
		
		HorizontalPanel rightAlignPanel = new HorizontalPanel();
		rightAlignPanel.setWidth("100%");
		
		rightAlignPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		rightAlignPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		//rightAlignPanel.add(fbShare);
		buttonHorzPanel.add(searchButton);
		buttonHorzPanel.setCellHorizontalAlignment(rightAlignPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		buttonHorzPanel.add(rightAlignPanel);
		//topPanel.add(searchButton);
		topPanel.add(buttonHorzPanel);
		topPanel.add(new HTML("&nbsp;"));
		panel = new VerticalPanel();
		panel.add(centralPanel);
		
		panel.setWidth("100%");
		searchBox.addKeyDownHandler(new KeyButtonHandler());
		basePanel.setStyleName("base-Panel");
		updateCentralPanel(msg);
		
		basePanel.add(topPanel, DockPanel.NORTH);
		basePanel.setCellHeight(topPanel, (Window.getClientHeight() - 30)+"px");
		basePanel.setCellWidth(topPanel, Window.getClientWidth()+"px");
		//basePanel.setCellHorizontalAlignment(topPanel, DockPanel.ALIGN_CENTER);
		//basePanel.setCellVerticalAlignment(topPanel, DockPanel.ALIGN_MIDDLE);
		
		searchButton.addClickHandler(new SearchButtonHandler());
		SearchValueChangeHandler historyHandler  = new SearchValueChangeHandler();
		History.addValueChangeHandler(historyHandler);
		initWidget(basePanel);
		String token = History.getToken();
		if(token.length() > 0){
			historyHandler.handleHistoryToken(token);
		}
	}
	@Override
	public void refreshLeftPanel(){
	//	leftPanel.add(new HTML("&nbsp;"));
	//	topPanel.add(new HTML("&nbsp;"));
	}
	@Override
	public void removeLeftPanel(){
		if(leftPanel != null && topPanel != null)
			topPanel.remove(leftPanel);
	}
	@Override
	public String processSummary(String summary){
		
		return summary;
    }
	@Override
	public String processSource(SearchResult s){
    	return s.getSource();
    }
	@Override
	public int getResultLsWidth(){
		return searchBox.getOffsetWidth();
	}
	@Override
	public boolean isSimilarSearchEnabled(){
		return false;
	}
	@Override
	public void setCentralPanelLoading(){
		if(centralPanel != null)
			centralPanel.remove(resultPanel);
		if(pagingPanel != null)
			centralPanel.remove(pagingPanel);
		if(bottomSearch != null)
			centralPanel.remove(bottomSearch);
		//resultPanel = new HTMLPanel("<p align='left'> <br/>Loading...</p>");
		//WebImages webImage = (WebImages) GWT.create(WebImages.class);
		//Image loading = webImage.loader().createImage();
		HTML loading = new HTML("<font size=\"+1\" color=\"#7fbce8\">Loading...</font>");
		resultPanel = new VerticalPanel();
		resultPanel.add(loading);
		VerticalPanel v = ((VerticalPanel)resultPanel);
		centralPanel.add(resultPanel);
		v.setCellWidth(loading, (2 *scrWidthResolution / 3)+"px");
		v.setCellHeight(loading, (2 *scrHeightResolution / 3)+"px");
		v.setCellHorizontalAlignment(loading, VerticalPanel.ALIGN_CENTER);
		v.setCellVerticalAlignment(loading, VerticalPanel.ALIGN_TOP);
	}
}
